package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierPlatPaytypeDao;
import com.xb.loan.cashier.biz.dao.bean.CashierPayChannel;
import com.xb.loan.cashier.biz.dao.bean.CashierPayChannelType;
import com.xb.loan.cashier.biz.dao.bean.CashierPlatPaytype;
import com.xb.loan.cashier.biz.service.CacheService;
import com.xb.loan.cashier.biz.service.CashierPayChannelService;
import com.xb.loan.cashier.biz.service.CashierPlatPaytypeService;
import com.xb.loan.cashier.biz.service.cache.RedisMethodCacheSupport;
import com.xb.loan.cashier.biz.service.channelselect.DefaultChannelSelectorFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("cashierPlatPaytypeService")
public class CashierPlatPaytypeServiceImpl implements CashierPlatPaytypeService, CacheService {

    @Autowired
    private CashierPlatPaytypeDao cashierPlatPaytypeDao;
    @Autowired
    private CashierPayChannelService cashierPayChannelService;

    @Override
    public CashierPlatPaytype queryEntity(String platId, String channelId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("platId", platId);
        params.put("channelId", channelId);
        return cashierPlatPaytypeDao.queryEntity(params);
    }

    @Override
    public CashierPlatPaytype selectByPlatAndChannelType(String platId, int paychannelType, int payType) {
        String[] paramsCache = new String[]{platId, paychannelType+""};
        CashierPlatPaytype type = RedisMethodCacheSupport.getBean(CashierPlatPaytype.class, paramsCache);
        if(type!=null){
            return type;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("platId", platId);
        params.put("paychannelType", paychannelType);
        params.put("payType", payType);
        type = cashierPlatPaytypeDao.selectByPlatAndChannelType(params);
        if(type==null){
            return null;
        }
        RedisMethodCacheSupport.setBean(CashierPlatPaytype.class, type, paramsCache);
        return type;
    }

    @Override
    public void clearCache(String methodName) {
        RedisMethodCacheSupport.clear(methodName);
    }

    @Override
    public void clearCacheAll() {
        RedisMethodCacheSupport.clearAll();
    }

    @Override
    public List<String> queryByPlatId(String platId) {
        List<String> list = RedisMethodCacheSupport.getBeans(String.class, platId);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        list = cashierPlatPaytypeDao.queryByPlatId(platId);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        RedisMethodCacheSupport.setBeans(String.class, list, platId);
        return list;
    }
    
    public static void main(String[] args) {
        new CashierPlatPaytypeServiceImpl().clearCacheAll();
        new CashierPlatPaytypeServiceImpl().clearCache("queryByPlatId");
    }

	@Override
	public CashierPlatPaytype selectByPlatAndChannelType(String platId,
                                                         List<CashierPayChannelType> paychannelType, int payType) {
		if(paychannelType == null || paychannelType.size() == 0 || StringUtils.isBlank(platId)){ //参数为空，则返回 null
			return null;
		}
		String paychannelTypeStr = "";
		for (CashierPayChannelType cashierPayChannelType : paychannelType) {
			if(paychannelTypeStr.equals("")){ //为空
				paychannelTypeStr = cashierPayChannelType.getPaychannelType() + "";
			}else{
				paychannelTypeStr = paychannelTypeStr + "," + cashierPayChannelType.getPaychannelType();
			}
		}
		CashierPlatPaytype type = null;
		/*String[] paramsCache = new String[]{platId, paychannelTypeStr+""};
        CashierPlatPaytype type = RedisMethodCacheSupport.getBean(CashierPlatPaytype.class, paramsCache);
        if(type != null){
            return type;
        }*/
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("platId", platId);
        params.put("paychannelTypeStr", paychannelTypeStr);
        params.put("payType", payType);
        // 查询出来所有符合调价你的支付类型
        List<CashierPlatPaytype> typeDB = cashierPlatPaytypeDao.selectByPlatAndChannelTypeArray(params);
        if(typeDB == null || typeDB.size() == 0){
            return null;
        }
        // 根据typeDB 拿到具体的权重  以找到唯一的type（CashierPlatPaytype）
        List<CashierPayChannel> listCashierPayChannel = new ArrayList<CashierPayChannel>();
        for (CashierPlatPaytype cashierPlatPaytype : typeDB) {
        	CashierPayChannel cashierPayChannel = cashierPayChannelService.queryByChannelId(cashierPlatPaytype.getChannelId());
        	if(cashierPayChannel != null){
        		listCashierPayChannel.add(cashierPayChannel);
        	}
        }
        // 根据支付渠道的权重，确定唯一的一个支付渠道
        CashierPayChannel cashierPayChannel =  (CashierPayChannel) DefaultChannelSelectorFactory.getInstance().getSelector().select(listCashierPayChannel);
        /*Map<String,Object> parMap = new HashMap<String,Object>();
        parMap.put("platId", platId);
        parMap.put("channelId", cashierPayChannel.getPayChannelId());*/
        for (CashierPlatPaytype cashierPlatPaytype : typeDB) {
			if(platId.equals(cashierPlatPaytype.getPlatId()) 
					&& cashierPayChannel.getPayChannelId().equals(cashierPlatPaytype.getChannelId())){
				type = cashierPlatPaytype;
				break;
			}
		}
        /*if(type != null){
        	RedisMethodCacheSupport.setBean(CashierPlatPaytype.class, type, paramsCache);
        }*/
        
        return type;
	}
}
